package com.cskaoyan.service;

import com.cskaoyan.mapper.MarketSystemMapper;
import com.cskaoyan.model.MarketSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author zoe ling
 * @Date 2024/6/8
 */
@Service
public class SystemServiceImpl implements SystemService {
    @Autowired
    MarketSystemMapper systemMapper;
    @Override
    public Map<String, String> toMap() {
        //example->查询全部记录
        List<MarketSystem> marketSystems = systemMapper.selectByExample(null);
        Map<String, String> configMap = marketSystems.stream().collect(Collectors.toMap(MarketSystem::getKeyName, MarketSystem::getKeyValue));

        return configMap;
    }

    @Override
    public BigDecimal getFreightPrice(BigDecimal checkedGoodsAmount) {
        BigDecimal freightPrice = BigDecimal.valueOf(0);
        Map<String, String> systemConfig = toMap();
        //最低免运费金额
        BigDecimal marketExpressFreightMin = BigDecimal.valueOf(Long.parseLong(systemConfig.get("market_express_freight_min")));
        //未满免运费时，运费金额
        BigDecimal marketExpressFreightValue = BigDecimal.valueOf(Long.parseLong(systemConfig.get("market_express_freight_value")));
        //商品金额小于等于免运费金额
        if (checkedGoodsAmount.compareTo(marketExpressFreightMin)==-1){
            freightPrice=freightPrice.add(marketExpressFreightValue);
        }
        return freightPrice;
    }
}
